package model

import (
	"context"
	"demo/internal/errorx"
	// "encoding/json"
	"fmt"

	// "fmt"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/cache"

	// "github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var _ ServeyModel = (*customServeyModel)(nil)

var insertServeyColumns = stringx.Remove(serveyFieldNames,"id")

type (
	// ServeyModel is an interface to be customized, add more methods here,
	// and implement the added methods in customServeyModel.
	ServeyModel interface {
		serveyModel
		Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
		InsertGetId(ctx context.Context, session sqlx.Session, data *Servey) (int64, error)
		InsertBuilder()squirrel.InsertBuilder
		SelectBuilder()squirrel.SelectBuilder
		GetListByPage(ctx context.Context,session sqlx.Session,sqSelect squirrel.SelectBuilder)([]*Servey,error)
		
	}

	customServeyModel struct {
		*defaultServeyModel
	}
)

// NewServeyModel returns a model for the database table.
func NewServeyModel(conn sqlx.SqlConn,cache cache.CacheConf) ServeyModel {
	return &customServeyModel{
		defaultServeyModel: newServeyModel(conn,cache),
	}
}

func (m *defaultServeyModel) Trans(ctx context.Context,fn func(ctx context.Context, session sqlx.Session)error)error{
	return m.cachedConn.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session)error{
		return fn(ctx, session)
	})
}

func (m *customServeyModel) GetListByPage(ctx context.Context,session sqlx.Session,sqSelect squirrel.SelectBuilder)([]*Servey,error){
	query,values,err := sqSelect.ToSql()
	if err != nil {
		return nil,errorx.NewBizError(500,"SQL代码生成错误")
	}
	// var value []byte 
	var data []*Servey
	// m.cachedConn.GetCacheCtx(ctx,"servey:search:serveyClassifyId:5",&value)
	// if len(value) > 0 {
	// 	err = json.Unmarshal(value,&data)
	// 	if err != nil{
	// 		fmt.Printf("反序列化出现错误",err)
	// 		return nil,errorx.NewBizError(500,"反序列化出现错误")
	// 	}
	// 	fmt.Println("缓存中查询到的数据是：",*data[0])
	// }else{
		if session != nil {
			err = session.QueryRowsPartialCtx(ctx,&data,query,values...)
		}else{
			err = m.cachedConn.QueryRowsNoCacheCtx(ctx,&data,query,values...)
		}
		if err != nil {
			fmt.Println("error is :",err)
			return nil,errorx.NewBizError(500,err.Error())
		}
	// 	byteData,err := json.Marshal(&data)
	// 	if err != nil {
	// 		return nil,errorx.NewBizError(500,"序列化时出错")
	// 	}
	// 	m.cachedConn.SetCacheCtx(ctx,"servey:search:serveyClassifyId:5",byteData)
	// }
	return data,nil
}

func (m *customServeyModel) InsertGetId(ctx context.Context, session sqlx.Session, data *Servey) (int64, error) {
	query, args, err := m.InsertBuilder().Columns(insertServeyColumns...).
		Values(data.CreatorId,data.Title,data.Description,data.CreateTime,data.PublishTime,data.EndTime,data.IsPublic,data.IsEnd,data.VisitNum,data.IsDeleted).
		Suffix("RETURNING \"id\"").
		ToSql()
	if err != nil {
		return 0, err
	}
	var id int64
	if session != nil {
		err = session.QueryRowPartialCtx(ctx, &id, query, args...)
	} else {
		err = m.cachedConn.QueryRowsNoCacheCtx(ctx, &id, query, args...)
	}
	if err != nil {
		return 0, err
	}

	return id,nil
}


func (m *customServeyModel) InsertBuilder()squirrel.InsertBuilder {
	return squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).Insert(m.table)
}

func (m *customServeyModel) SelectBuilder()squirrel.SelectBuilder {
	return squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).Select("servey.*").From(m.table)
}
